import Vuex from "vuex";
import Vue from "vue";

Vue.use(Vuex);
const store = new Vuex.Store({
  state: {
    count: 0,
    list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
  },
  mutations: {
    addCount(state, payload) {
      state.count += payload;
    },
    addFn(state, payload) {
      state.count += payload;
    },
  },
  actions: {
    getAsyncCount(context, payload) {
      setTimeout(function() {
        context.commit("addCount", payload);
      }, 1000);
    },
  },
  getters: {
    filterList: (state) => {
      return state.list.filter((item) => item > 5);
    },
    token: (state) => state.user.token,
    name: (state) => state.setting.name,
  },
  modules: {
    user: {
      namespaced: true,
      state: {
        token: "阿思阿思",
      },
      mutations: {
        //  这里的state表示的是user的state
        updateToken(state) {
          state.token = "你哪里走";
        },
      },
    },
    setting: {
      state: {
        name: "Vuex实例",
      },
    },
  },
});
export default store;
